﻿//begin ready
$(document).ready(function () {
    AverageBrand();

    var htmlval = $("#wrapToggle").html();
    $("#wrapToggle").html(htmlval);
    $("#uploadify").uploadify({
        'uploader': 'media/js/jquery.uploadify-v2.1.4/uploadify.swf',
        'script': 'ServerAshxFile/JqueryUploadHandler.ashx',
        'scriptData': { 'custuid': $("#ContentPlaceHolder2_hidCustomerUID").attr("value"), 'type': 'brandlogo' },
        'method': 'GET',
        'cancelImg': 'media/js/jquery.uploadify-v2.1.4/cancel.png',
        'folder': 'media/upload/brandlogo',
        'queueID': 'fileQueue',
        'auto': false,
        'multi': false,
        'displayData': 'speed',
        'fileExt': '*.jpg;*.gif;*.png;*.bmp',
        'fileDesc': 'Image Files',
        'sizeLimit': 102400,
        'onInit': function () { },
        'onSelect': function (e, queueId, fileObj) {
            $('#id_message').html("文件名:" + fileObj.name + "\r\n" +
                              "文件大小:" + fileObj.size + "bytes \r\n" +
                              "文件类型:" + fileObj.type
                              );
        },
        'onAllComplete': function (event, data) {
            $("#ContentPlaceHolder2_btnRefreshImgList").click();
            setTimeout(function () {
                var htmlval = $(".slist").html();
                $(".slist").html(htmlval);
                $(".slist li:first-child").children("span").click();
            }, 1000);
        }
    });

    $("#menu1_ul1").find("li").each(function (i) {
        $(this).click(function () { $(this).children("span").toggleClass("span" + (i + 1)); });
    });
    $("#menu1_ul1").find(".li1").children("span").addClass("span1");
    $("#startwith_A").css("display", "block");


    //产品大分类 菜单
    $("#menu1_ul1").find("li").each(function (i) {
        $(this).click(function () {
            var AllUID = $(this).attr("id").replace("menu_ul_", "");
            $("#menu1_ul1").find("li").children("span").removeAttr("class");
            $(this).children("span").addClass("span" + (i + 1));
            display_productcategory(AllUID);
        });
    });

    //ALL checked事件
    $(".clsckbAll").click(function () {
        var allID = $(this).attr("id");
        if ($(this).attr("checked") == true) {
            $("#startwith_" + allID).find("input").attr('checked', true);
        }
        else {
            $("#startwith_" + allID).find("input").attr('checked', false);
        }
        SetSelectedCss(this);
    });
    //End

    //大类别 checked事件
    $(".ckb_cat").click(function () {
        var catuid = $(this).attr("value");
        // 子类
        if ($(this).attr("checked") == true) {
            $("#ul_cat_" + catuid + " li").find("input").attr("checked", true);
        }
        else {
            $("#ul_cat_" + catuid + " li").find("input").attr("checked", false);
        }
        // All
        var check = true;
        $(this).parents("ul").find("input").each(function () {
            if ($(this).attr("checked") == false) {
                check = false;
                return false; //跳出循环
            }
        });
        if (check) {
            $("#" + $("#ckb_cat_" + catuid).parents("ul").attr("id").replace("cat_", "")).attr("checked", true);
        }
        else { $("#" + $("#ckb_cat_" + catuid).parents("ul").attr("id").replace("cat_", "")).attr("checked", false); }

        SetSelectedCss(this);  //设置selected css
    });
    // End

    //SubCat Click事件
    $(".ckb_subcat").click(function () {
        var check = true;
        //大类是否需要被checked
        var parentuid = $(this).parents("ul").attr("id").replace("ul_cat_", "");
        $(this).parents("ul").find("input").each(function () {
            if ($(this).attr("checked") == false) {
                check = false;
                return false; //跳出循环
            }
        });
        if (check) {
            $("#ckb_cat_" + parentuid).attr("checked", true);
        }
        else {
            $("#ckb_cat_" + parentuid).attr("checked", false);
        }
        //设置该input所在的All的selected Css
        SetSelectedCss(this);

    });
    //End

    //begin checkboxList赋值
    var strSubUID = $("#ContentPlaceHolder2_hidRelatedBrandProdCatStrUID").attr("value");
    if (strSubUID != "") {
        var strSubUIDList = strSubUID.substring(0, strSubUID.length - 1).split(",");
        if (strSubUIDList.length > 0) {
            $.each(strSubUIDList, function (key, thisval) {
                //$(".ckb_subcat[value='" + thisval + "']").attr("checked", true); //配合ReadySetCss函数才能在ready正确修改menu样式。
                $(".ckb_subcat[value='" + thisval + "']").click();
            });
            ReadySetCss(strSubUIDList);
        }
    }
    //end

    $.each(["A", "B", "C", "D", "E"], function (index, vals) {
        var AllCheck = true;
        $("#cat_" + vals + " .ckb_cat").each(function () {
            var CatDom = $(this);
            var CatUID = CatDom.attr("value");
            var CatCheck = true;
            $("#ul_cat_" + CatUID + " li").find("input").each(function () {
                if ($(this).attr("checked") == false) {
                    CatCheck = false;
                    return false;
                }
            });
            if (CatCheck == false) {
                AllCheck = false;
            }
            else {
                CatDom.attr("checked", true);
            }
        });
        if (AllCheck == true) {
            $("#" + vals).attr("checked", true);
        }
    });
    var cbuid = $("#ContentPlaceHolder2_hidBrandUID").val();
    if (cbuid > 0) {
        ToggleDocument('wrapToggle');
    }
});
//end ready
function AddBrand() {
    ToggleDocument('wrapToggle');
}

function QuitToAdd()
{ ClearText(); ToggleDocument('wrapToggle'); }

function ClearText() {

    $(".ImgList").html("");
    var cuid = $("#ContentPlaceHolder2_hidCustomerUID").val();
    var options = $("#ContentPlaceHolder2_hidCustomerUID").val();
    $("#wrapToggle input[type='hidden']").val("");
    $("#ContentPlaceHolder2_hidCustomerUID").val(cuid);
    $("#ContentPlaceHolder2_hidCustomerUID").val(options);
    $("#wrapToggle input[type='text']").val("");

    $("#wrapToggle textarea").val("");
    $("#wrapToggle .editMode").html("");
    $(".productWrap input:checked").attr("checked", false);
    $("#menu1_ul1 .selected").css("display", "none");

}
function ReadySetCss(strlist) {
    var isHasChecked = false;
    $.each(strlist, function (index, thisval) {
        var AllUID = $(".ckb_subcat[value='" + thisval + "']").parents(".clsAllcheck").attr("id").replace("startwith_", "");
        $("#menu_ul_" + AllUID).children("div").attr("style", "display:block;");
    });
}

//设置该input所在的All的selected Css
function SetSelectedCss(docval) {
    var isHasChecked = false;
    var AllUID = $(docval).parents(".clsAllcheck").attr("id").replace("startwith_", "");
    $(docval).parents(".clsAllcheck").find("input").each(function () {
        if ($(this).attr("checked") == true) {
            isHasChecked = true;
            return false; //跳出循环
        }
    });
    if (isHasChecked) {
        $("#menu_ul_" + AllUID).children("div").attr("style", "display:block;");
    }
    else {
        $("#menu_ul_" + AllUID).children("div").attr("style", "display:none;");
    }
}
//End

//隐藏所有 显示特定的
function display_productcategory(i) {
    $(".clsAllcheck").css("display", "none");
    $("#startwith_" + i).css("display", "block");
}

function GetCatInfo() {
    $("#ContentPlaceHolder2_lblMeg").html("");
    if ($("#ContentPlaceHolder2_lkBtnSave").hasClass("aspNetDisabled").toString() != "true") {
        $("#ContentPlaceHolder2_lblMeg").html("数据正在处理,请稍候...");
        var strChecked = "";
        //找出需要增加的UID
        $(".ckb_subcat:checked").each(function () {
            var checkval = $(this).val();
            strChecked += checkval + ",";
        });
        var engVal = $("#ContentPlaceHolder2_txtBrandIntro_Eng").val();
        $("#ContentPlaceHolder2_txtBrandIntro_Eng").val(engVal);
        var engVal = $("#ContentPlaceHolder2_txtBrandIntro_Chi").val();
        $("#ContentPlaceHolder2_txtBrandIntro_Chi").val(engVal);
        $("#ContentPlaceHolder2_hidRelatedBrandProdCatStrUID").val("").val(strChecked);
    }
    return ret;
}


//图片相关-----------
function AddImg(type) {
    $('#id_message').html("");
    ToggleDocument('smallImgList2');
}
function EditImgRelated(thisval) {
    $('#id_message').html("");
    ToggleDocument('smallImgList2');
}
// 关联操作
function ToRelated(thisval) {
    var fileName = $(thisval).find("img").attr("alt");
    var ImgUrl = "/media/upload/brandlogo/" + fileName + "_150_150.jpg";
    var imguid = $(thisval).find("img").attr("class");
    if ($(".ImgList").find("." + imguid).size() == 0) {
        var AddedHtml = "<li class=\"" + imguid + "\"><p class=\"p2\"><img src=\"" + ImgUrl + "\" /></p><div class=\"p4\"><span class=\"showbig\" onclick=\"EditImgRelated(this)\">Edit</span> | <span class=\"closeBtnbig\" onclick=\"DeleteRelated(this)\">Delete</span></div></li>";
        $(".ImgList").html(AddedHtml);
        $("#ContentPlaceHolder2_hidMediaRoomStr").val(imguid);
    }
    else {
        alert("图片：" + fileName + "\n已被添加到列表中.");
    }
}

function DeleteRelated(thisval) {
    $(thisval).parents("li").remove();
    $("#ContentPlaceHolder2_hidMediaRoomStr").val("");
}
function DeleteBrand() {
    var cbuid = args1;
    $.ajax({
        url: "/ServerAshxFile/M_DeleteBrand.ashx",
        data: { CUID: $("#ContentPlaceHolder2_hidCustomerUID").val(), CBUID: cbuid },
        beforeSend: function () { },
        success: function (str) {
            var json = { "retValue": "[" + str.toString() + "]" };
            json = eval(json.retValue);
            if (json[0].Issucess == "1") {
                $("#ContentPlaceHolder2_btnRefreshBrandList").click();

            }
            else {
                //输出信息
            }
        }
    });
}